


package jnpf.fico.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.fico.entity.ZcglCwInvoiceEntity;
import jnpf.fico.model.invoice.ZcglCwInvoiceForm;
import jnpf.fico.model.invoice.ZcglCwInvoiceInfoVO;
import jnpf.fico.model.invoice.ZcglCwInvoiceListVO;
import jnpf.fico.model.invoice.ZcglCwInvoicePagination;
import jnpf.fico.service.ZcglCwInvoiceService;
import jnpf.permission.entity.UserEntity;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * ZcglCwInvoice
 *
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-10-30
 */
@Slf4j
@RestController
@Api(tags = "ZcglCwInvoice", value = "example")
@RequestMapping("/api/example/ZcglCwInvoice")
public class ZcglCwInvoiceController {


    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcglCwInvoiceService zcglCwInvoiceService;


    /**
     * 列表
     *
     * @param zcglCwInvoicePagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglCwInvoicePagination zcglCwInvoicePagination) throws IOException {
        List<ZcglCwInvoiceEntity> list = zcglCwInvoiceService.getList(zcglCwInvoicePagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<ZcglCwInvoiceListVO> listVO = JsonUtil.getJsonToList(list, ZcglCwInvoiceListVO.class);
        for (ZcglCwInvoiceListVO zcglCwInvoiceVO : listVO) {
            zcglCwInvoiceVO.setInvoiceType_name(zcglCwInvoiceVO.getInvoiceType());

            zcglCwInvoiceVO.setApplyTime_name(zcglCwInvoiceVO.getApplyTime());

            zcglCwInvoiceVO.setApplyAmount_name(zcglCwInvoiceVO.getApplyAmount());

            zcglCwInvoiceVO.setPayer_name(zcglCwInvoiceVO.getPayer());

            zcglCwInvoiceVO.setExpenseItem_name(zcglCwInvoiceVO.getExpenseItem());

            zcglCwInvoiceVO.setPlanReceiptDate_name(zcglCwInvoiceVO.getPlanReceiptDate());

            zcglCwInvoiceVO.setPaymentInfo_name(zcglCwInvoiceVO.getPaymentInfo());

            zcglCwInvoiceVO.setApplicantName_name(zcglCwInvoiceVO.getApplicantName());

            zcglCwInvoiceVO.setStatus_name(zcglCwInvoiceVO.getStatus());

            zcglCwInvoiceVO.setFullName_name(zcglCwInvoiceVO.getFullName());

            //子表数据转换
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zcglCwInvoicePagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    @ApiOperation("获取发票管理列表")
    @PostMapping("/getListPage")
    public ActionResult getListPage(@RequestBody ZcglCwInvoicePagination pagination) throws IOException {
        return ActionResult.success(zcglCwInvoiceService.getListPage(pagination));
    }


    /**
     * 创建
     *
     * @param zcglCwInvoiceForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcglCwInvoiceForm zcglCwInvoiceForm) throws DataException {
        boolean b = zcglCwInvoiceService.checkForm(zcglCwInvoiceForm, 0);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcglCwInvoiceEntity entity = JsonUtil.getJsonToBean(zcglCwInvoiceForm, ZcglCwInvoiceEntity.class);
        entity.setId(mainId);
        zcglCwInvoiceService.save(entity);


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglCwInvoiceInfoVO> info(@PathVariable("id") String id) {
        ZcglCwInvoiceEntity entity = zcglCwInvoiceService.getInfo(id);
        ZcglCwInvoiceInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglCwInvoiceInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 发票管理表单信息(详情页)
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcglCwInvoiceInfoVO> detailInfo(@PathVariable("id") String id) {
        ActionResult actionResult = zcglCwInvoiceService.detailInfo(id);
        return actionResult;
    }

    /**
     * 提交
     *
     * @param zcglCwInvoiceForm
     * @return
     */
    @PostMapping("/commit")
    @Transactional
    @ApiOperation("提交")
    public ActionResult commit(@RequestBody @Valid ZcglCwInvoiceForm zcglCwInvoiceForm) throws DataException {
        ActionResult actionResult = zcglCwInvoiceService.commit(zcglCwInvoiceForm);
        return actionResult;
    }


    /**
     * 更新
     *
     * @param id
     * @param zcglCwInvoiceForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZcglCwInvoiceForm zcglCwInvoiceForm) throws DataException {
        boolean b = zcglCwInvoiceService.checkForm(zcglCwInvoiceForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo = userProvider.get();
        ZcglCwInvoiceEntity entity = zcglCwInvoiceService.getInfo(id);
        if (entity != null) {
            ZcglCwInvoiceEntity subentity = JsonUtil.getJsonToBean(zcglCwInvoiceForm, ZcglCwInvoiceEntity.class);
            boolean b1 = zcglCwInvoiceService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZcglCwInvoiceEntity entity = zcglCwInvoiceService.getInfo(id);
        if (entity != null) {
            zcglCwInvoiceService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


}
